package MyArrayList;

public class MyArrayList {
    Object[] values;
    int length;
    int size;
    final int def_len = 10;

    public MyArrayList(){
        length = def_len;
        values = new Object[length];
        size = 0;
    }

    public MyArrayList(int initCap){
        if (initCap < 2){
            System.out.println("输入容器不得小于1");
            return;
        }
        length = initCap;
        values = new Object[length];
        size = 0;
    }

    public int size(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    public void add(Object e){
        if (size == length){ //扩容
            int oldLen = length;
            int newLen = oldLen + (oldLen >> 1);
            Object[] newArr = new Object[newLen];
            for (int i = 0; i < oldLen; i++) {
                newArr[i] = values[i];
            }
            values = newArr;
            length = newLen;
            System.out.println("add扩容完成");
        }
        values[size] = e;
        size++;
    }

    public void addAll(Object[] es){
        if (size + es.length >= length){
            int oldLen = length + es.length;
            int newLen = oldLen + (oldLen >> 1);
            Object[] newArr = new Object[newLen];
            for (int i = 0; i < length; i++) {
                newArr[i] = values[i];
            }
            values = newArr;
            length = newLen;
            System.out.println("addAll扩容完成");
        }
        int t = 0;
        for (int i = size; i < size + es.length; i++) {
            values[i] = es[t];
            t++;
        }
        size = size + es.length;
    }

    public Object get(int index){
        if (index < 0 || index >= size){ //size而不是length，因为要考虑数据是否存在
            System.out.println("输入的下标越界");
        }
        Object value = values[index];
        return value;
    }

    public Object remove(int index){
        if (index < 0 || index >= size){
            System.out.println("输入的下标越界");
        }
        Object temp = values[index];
        for (int i = index; i < size - 1; i++) {
            values[i] = values[i + 1];
        }
        values[size - 1] = null;
        size--;
        return temp;
    }

    public void remove(int si , int ei){
        if (si < 0 || ei >= size){
            System.out.println("输入的下标越界");
        }
        if (size - ei >= ei - si){
            for (int i = si; i < size; i++) {
                values[i] = values[i + ei - si + 1];
            }
        }else {
            for (int i = 0; i < size - ei; i++) {
                values[si] = values[ei + 1];
            }
        }
        for (int i = 0; i < size - ei + 1; i++) {
            values[size - i] = "";
        }
        size = size - (ei - si + 1);
    }
    
    public void remove(Object o){
        int n = 0;
        for (int i = 0; i < size; i++) {
            if (values[i].equals(o)){
                n++;
            }else {
                values[i - n] = values[i];
            }
        }
        if (n == 0){
            System.out.println("不存在该元素");
        }
        size = size - n;
    }

    public void replace(Object oldE, Object newE) {
        for (int i = 0; i < size; i++) {
            if (values[i].equals(oldE)){//主要要用equals()
                values[i] = newE;
            }
        }
    }

    public void set(int index, Object e) {
        if (size == length){ //扩容
            int oldLen = length;
            int newLen = oldLen + (oldLen >> 1);
            Object[] newArr = new Object[newLen];
            for (int i = 0; i < oldLen; i++) {
                newArr[i] = values[i];
            }
            values = newArr;
            length = newLen;
            System.out.println("扩容完成");
        }
        Object[] tempList = new Object[length];
        for (int i = 0; i <= index; i++) {
            tempList[i] = values[i];
        }
        tempList[index + 1] = e;
        for (int i = index + 1; i < size; i++) {
            tempList[i + 1] = values[i];
        }
        values = tempList;
        size++;
    }

    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        for (int i = 0; i < 20; i++) {
            myArrayList.add("hello" + i);
        }

        Object[] objects = new Object[5];
        for (int i = 0; i < 5; i++) {
            objects[i] = "多添加" + i;
        }
        myArrayList.addAll(objects);
        Object oj;
        oj = "单添加 1";
        myArrayList.add(oj);

        for(int i = 0; i < myArrayList.size; i++) {
            System.out.println(myArrayList.get(i));
        }

        System.out.println("---------------------");

        myArrayList.remove(2);
        myArrayList.remove(10,14);

        Object oj1 = "hello" + 0;
        Object oj2 = "hello" + 100;
        myArrayList.replace(oj1,oj2);

        Object o = "插入1";
        myArrayList.set(4, o);

        for(int i = 0; i < myArrayList.size; i++) {
            System.out.println(myArrayList.get(i));
        }

    }


}




